-20, 19, 1, 5, -1, 27, 19, 5
A systematic procedure used to compute something is called an algorithm. The procedure described in the answer is an algorithm for finding the maximum of a list. An algorithm is a description of how to do something; it is not tied to any particular language. For example, you (possibly) followed the above algorithm when you mentally examined the list. An algorithm can be implemented in any computer programming language. Here is the above algorithm implemented in Java:
class MaxAlgorithm
{
public static void main ( String[] args )
{
int[] array = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
int max;
// initialize the current maximum
max = ____________________
// scan the array
for ( int index=0; index < array.length; index++ )
{
<more stuff goes here>
}
System.out.println("The maximum of this array is: " + max );
}
}
The variable max
should be initialized to a value that is
guaranteed to be no larger than the maximum element of the array.
For a program that will always work, regardless of the quirks of the
data, you should make no assumptions about what the data look like.
(The initializer list is only for a convenience in this example;
a typical program would get its data from the user or from a file.)